<?php

/**
 * uniBoard ACP Maintenance Tasks
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_tasks{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Add language string
		$appCore -> output -> js_vars['l_delete_item_sure'] =  $appCore -> lang['delete_item_sure'];
			
		// Load fields templates
		$appCore -> templates['form_inputs'] = new tpl_form_inputs( $appCore);
		
		// Cache output
		ob_start();
		
		// Enable Task?
		if ( $appCore -> _REQ( 'do') == 'on')
		{
			// Make Api
			$task_api = new api_task( $appCore, $appCore -> _REQ( 'task'));
			
			// Task found?
			if ( $task_api -> data_loaded)
			{
				// Enable task
				$task_api -> setTaskActive( true);
				$task_api -> saveData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_enabled_task', htmlspecialchars( $task_api -> task_name));
				
				// Draw Message
				$appCore -> output -> message( $appCore -> lang['acp_task_turned_on'], $appCore -> ifString( htmlspecialchars( $task_api -> task_name)));
			}
			else
			{
				// Show error
				$appCore -> output -> error( $appCore -> lang['acp_task_not_found'], $appCore -> lang['acp_tasks']);
			}
		}
		
		// Disable Task?
		if ( $appCore -> _REQ( 'do') == 'off')
		{
			// Make Api
			$task_api = new api_task( $appCore, $appCore -> _REQ( 'task'));
			
			// Task found?
			if ( $task_api -> data_loaded)
			{
				// Enable task
				$task_api -> setTaskActive( false);
				$task_api -> saveData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_disabled_task', htmlspecialchars( $task_api -> task_name));
				
				// Draw Message
				$appCore -> output -> message( $appCore -> lang['acp_task_turned_off'], $appCore -> ifString( htmlspecialchars( $task_api -> task_name)));
			}
			else
			{
				// Show error
				$appCore -> output -> error( $appCore -> lang['acp_task_not_found'], $appCore -> lang['acp_tasks']);
			}
		}
		
		// Open table block
		echo $appCore -> output -> openTableBlock();
		
		// Draw header
		echo $appCore -> output -> tableHeader( array(
			array(
				'left_float width_half',
				$appCore -> lang['acp_tasks_list_task']
			),
			array(
				'right_float width_medium',
				$appCore -> lang['actions']
			),
			array(
				'right_float width_big',
				$appCore -> lang['acp_tasks_list_next_run']
			)
		));
				
		// Select Tasks
		$appCore -> db -> query( 'SELECT task_id, task_name, task_description, task_next_run, task_active
		FROM ' . DB_PREFIX . 'tasks
		ORDER BY task_active DESC, task_next_run DESC');
		
		// Parse result
		while ( $task_result = $appCore -> db -> fetch_array())
		{
			// Draw row
			echo $appCore -> output -> tableRow('
				<div class="cell left_float width_tiny center">
					<a href="' . $appCore -> adminLink( 'act=tasks&do=' . ( $task_result[4] ? 'off' : 'on') . '&task=' . $task_result[0]) . '">' . $appCore -> output -> drawImage( 'state_' . $task_result[4], $appCore -> lang['acp_tasks_list_task_active_' . $task_result[4]]) . '</a>
				</div>
				<div class="cell left_border left_float width_half">
					<b>' . $appCore -> ifString( htmlspecialchars( $task_result[1])) . '</b>' . ( isset( $task_result[2][0]) ? '<div class="item_description">' . $appCore -> ifString( htmlspecialchars( $task_result[2])) . '</div>' : '') . '
				</div>
				<div class="cell left_border right_float width_medium center">
					<a href="' . $appCore -> adminLink( 'act=edit_task&do=edit&task=' . $task_result[0]) . '">' . $appCore -> output -> drawImage( 'action_edit', $appCore -> lang['edit']) . '</a>
					<a href="' . $appCore -> adminLink( 'act=edit_task&do=delete&task=' . $task_result[0]) . '" class="delete_confirm">' . $appCore -> output -> drawImage( 'action_delete', $appCore -> lang['delete']) . '</a>
				</div>
				<div class="cell left_border right_float width_big">
					' . ( $task_result[3] > 0 ? $appCore -> getDate( $task_result[3]) : $appCore -> lang['date_now']) . '
				</div>');
		}
				
		// Free result
		$appCore -> db -> freeResult();
		
		// Close table block
		echo $appCore -> output -> closeTableBlock();
		
		// New Task Button
		echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=new_task'));
		echo $appCore -> output -> openFormBlock( $appCore -> lang['new_task_form']);
		echo $appCore -> output -> submitButton( $appCore -> lang['new_task_form_submit']);
		echo $appCore -> output -> closeFormBlock();
		echo $appCore -> output -> closeForm();
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['acp_tasks'], array( $appCore -> adminLink( 'act=tasks') => $appCore -> lang['acp_tasks']), ob_get_clean());
				
	}

}